package ma.ensias.healthme.admin;

import java.util.ArrayList;
import java.util.List;

import ma.ensias.healthme.R;
import ma.ensias.healthme.entities.Maladie;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
public class MaladieLazyAdapter extends BaseAdapter {
    
    private Activity activity;
    private ArrayList<Maladie> data;
    private ArrayList<Maladie> checkedMaladies=new ArrayList<Maladie>();
    private static LayoutInflater inflater=null;
    
    public MaladieLazyAdapter(Activity a, ArrayList<Maladie> d) {
        activity = a;
        data=d;
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public int getCount() {
        return data.size();
    }

    public Object getItem(int position) {
        return data.get(position);
    }

    public long getItemId(int position) {
        return position;
    }
    
    public View getView(final int position, View convertView, ViewGroup parent) {
		View vi=convertView;
		if(convertView==null)
		 vi = inflater.inflate(R.layout.maladie_row, null);
		
		CheckBox check = (CheckBox)vi.findViewById(R.id.checkBox); // title
		Maladie m=data.get(position);
		check.setText("\t"+m.getNom());
		
		check.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
				if(arg1==true){
					checkedMaladies.add(data.get(position));
				}
				else
					checkedMaladies.remove(data.get(position));
			}
		});
		
		return vi;
    }
    
    public List<Maladie> getSelectedMaladies(){
    	return checkedMaladies;
    }
}